package adapter;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.example.administrator.ci_edit.R;

import java.util.List;

import db.CipaiDatabaseHelper;
import entity.MyWork;


public class MyWorkAdapter extends ArrayAdapter<MyWork> {
    private int resourceId;

    public MyWorkAdapter(Context context, int textViewResourceId, List<MyWork> objects) {
        super(context, textViewResourceId, objects);
        resourceId = textViewResourceId;
    }

    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        MyWork mywork = getItem(position);
        View view;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);
        } else {
            view = convertView;
        }
        //ImageView cipaiImage=(ImageView)view.findViewById(R.id.cipai_image);
        TextView myworkName = view.findViewById(R.id.mywork_name);
        //cipaiImage.setImageResource(cipaiInfo.getImageId());
        myworkName.setText(mywork.getWorkname());

        String purename = getPureName(mywork.getWorkname());


        CipaiDatabaseHelper cipaiDatabaseHelper = new CipaiDatabaseHelper();
        Cursor cursor = cipaiDatabaseHelper.getRGBByCipaiName(purename);
        while (cursor.moveToNext()){
            int red = cursor.getInt(cursor.getColumnIndex("red"));
            int green = cursor.getInt(cursor.getColumnIndex("green"));
            int blue = cursor.getInt(cursor.getColumnIndex("blue"));
            myworkName.setTextColor(Color.rgb(red,green,blue));
        }
        return view;
        //return super.getView(position, convertView, parent);
    }

    private String getPureName (String rawname){
        String [] ss = rawname.split("[0-9][0-9]");
        String [] sss = ss[5].split("\\.");
        //System.out.println(sss[0]);
        return (sss[0]);
    }
}
